﻿using System;
using PEProtocol;

/**
 * 计时服务
 */
public class TimerSvc : SystemRoot{
    public static TimerSvc Instance = null;

    private PETimer pt;

    public void InitSvc(){
        Instance = this;
        pt = new PETimer();
        //设置日志
        pt.SetLog((string info) => {
            PECommon.Log(info);
        });

        GameMsg msg = new GameMsg{
            cmd = (int) CMD.ReqHeart
        };
        //开始持续心跳包的发送
        AddTimerTask((int tid) => {
            NetSvc.Instance.SendMsg(msg);
            //PECommon.Log("发送心跳包");
        }, 5000, PETimeUnit.Millisecond, -1);
        PECommon.Log("Init TimerSvc...");
    }

    private void Update(){
        pt.Update();
    }

    public void AddTimerTask(Action<int> cb, double delay, PETimeUnit timeUnit = PETimeUnit.Millisecond, int count = 1){
        pt.AddTimeTask(cb, delay, timeUnit, count);
    }

    public double GetNowTime(){
        return pt.GetMillisecondsTime();
    }
}